package com.onairm.baselibrary.mvp.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;

import com.onairm.baselibrary.mvp.activity.ActivityMvpDelegate;
import com.onairm.baselibrary.mvp.activity.ActivityMvpDelegateImpl;
import com.onairm.baselibrary.mvp.base.MvpBasePresenter;
import com.onairm.baselibrary.mvp.base.MvpCallback;
import com.onairm.baselibrary.mvp.base.MvpPresenter;
import com.onairm.baselibrary.mvp.base.MvpView;

/**
 * Created by bqy on 2018/3/27.
 */

public abstract class MvpActivity<V extends MvpView, P extends MvpPresenter<V>> extends
        FragmentActivity implements MvpView, MvpCallback<V, P> {

    private ActivityMvpDelegate<V, P> mvpDelegate;

    public ActivityMvpDelegate<V, P> getMvpDelegate() {
        if (this.mvpDelegate == null) {
            this.mvpDelegate = new ActivityMvpDelegateImpl<V, P>(this);
        }
        return mvpDelegate;
    }

    private P presenter;

    @Override
    public P createPresenter() {
        return presenter;
    }

    @Override
    public P getPresenter() {
        return presenter;
    }

    @Override
    public void setPresenter(P presenter) {
        this.presenter = presenter;
    }

    @Override
    public V getMvpView() {
        return (V) this;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getMvpDelegate().onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getMvpDelegate().onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMvpDelegate().onResume();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getMvpDelegate().onRestart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        getMvpDelegate().onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        getMvpDelegate().onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getMvpDelegate().onDestroy();
    }
}
